﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;
using LTP.Accounts.Bus;


namespace lcs.Web.demandplaninfo
{
    public partial class List : Web.Admin.BasePage
    {
		lcs.BLL.demandplaninfo bll = new lcs.BLL.demandplaninfo();
        BLL.demandmaterialinfo dbll = new BLL.demandmaterialinfo();
        public string dogid = "NO";
        public string Decode(string str)
        {

            if (dogid == "NO")
            {
                if (Request.Cookies["dogid"]!=null)
                    dogid = Request.Cookies["dogid"].Value.ToString();
            }
            return DecodeDog(str, dogid);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //gridView.BorderColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_bordercolorlight"].ToString());
                //gridView.HeaderStyle.BackColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString());
                btnDelete.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？\")");
                BindData();
            }
        }
        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
        
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0) 
                return;
            bll.DeleteList(idlist);
            BindData();
        }
        
        #region gridView
                        
        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion
            if (IsLogin())
            {
                DataSet ds = new DataSet();
                StringBuilder strWhere = new StringBuilder();
                if (txtKeyword.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and pbh like '%{0}%'", txtKeyword.Text.Trim());
                }
                strWhere.AppendFormat(" and uid='{0}'", getid());
                ds = bll.GetList(strWhere.ToString()+" order by inputtime desc");
                gridView.DataSource = ds;
                gridView.DataBind();
            }
            else
            {
                Response.Redirect("~/jump.html");
            }
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");
                HyperLink hlf = (HyperLink)e.Row.FindControl("hlsfile");
                if (hlf != null)
                {
                    if (hlf.NavigateUrl.Trim() == "")
                        hlf.Visible = false;
                }
                //有物料了编辑就不能用了。
                HyperLink hledit = (HyperLink)e.Row.FindControl("HyperLink1");
                if(hledit!=null)
                {
                    if (dbll.GetRecordCount("dpid=" + ((DataRowView)e.Row.DataItem)["dpid"].ToString()) > 0)
                    {
                        hledit.Visible = false;
                        //linkbtnDel.Visible = false;
                    }
                }


                Label lbl_dpid = (Label)e.Row.FindControl("lbl_dpid");
                Label lbl_type = (Label)e.Row.FindControl("lbl_type");
                Label lbl_warn = (Label)e.Row.FindControl("lbl_warn");
                CheckBox cbo_deleteThis = (CheckBox)e.Row.FindControl("DeleteThis");//删除选择框

                HyperLink hy_submit = (HyperLink)e.Row.FindControl("hy_submit");//提交审核
                LinkButton hy_print = (LinkButton)e.Row.FindControl("hy_print");//打印
                HyperLink hy_wlgl = (HyperLink)e.Row.FindControl("hy_wlgl");//物料管理

                string dpid = lbl_dpid.Text;
                string type = lbl_type.Text;

                hy_wlgl.NavigateUrl = "../demandmaterialinfo/List.aspx?id=" + dpid + "&type="+type;

                Maticsoft.BLL.documentFlowInfo dfm = new Maticsoft.BLL.documentFlowInfo();
                Maticsoft.Model.documentFlowInfo d = dfm.GetModelList(" document_id = " + dpid)[0];

                //Maticsoft.BLL.documentFlowMapping dmap = new Maticsoft.BLL.documentFlowMapping();
                //List<Maticsoft.Model.documentFlowMapping> li_dm = dmap.GetModelList(" flow_id=" + d.flow_id
                //    + " and documentFlowMapping_name = '" + d.documentFlowMapping_name + "' and flow_op = '审核' ");

                //if (li_dm.Count == 1)//找到此流程图中的"审核"部分
                //{
                 
                //}

                //status 字段只要审核一次就会变成1，不管是否在哪个流程，即使在中间流程中的第一步，只要不是第一步流程的第一步都会是1
                if (d.isFlowDone)
                {
                    hy_print.Visible = true;
                    hy_submit.Visible = false;
                    lbl_warn.Text = "审核完毕";
                    hy_wlgl.Visible = false;
                    cbo_deleteThis.Visible = true;
                }
                else
                {
                    hy_print.Visible = false;
                    if (d.status == 0)
                    {
                        hy_submit.Visible = true;
                        lbl_warn.Text = "未审核";
                        hy_wlgl.Visible = true;
                        cbo_deleteThis.Visible = true;
                    }
                    else if (d.status == 3)//审核未通过
                    {
                        hy_submit.Visible = false;
                        lbl_warn.Text = "审核未通过";
                        hy_wlgl.Visible = false;
                        cbo_deleteThis.Visible = false;
                    }
                    else
                    {
                        hy_submit.Visible = false;
                        lbl_warn.Text = "审核中";
                        hy_wlgl.Visible = false;
                        cbo_deleteThis.Visible = false;
                    }
                }
               
            }
        }
        
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            bll.Delete(ID);
            BindData();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {                        
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "print")
            {
                string dpid = e.CommandArgument.ToString();

                ClientScript.RegisterStartupScript(this.GetType(), "",
                    "<script>OpenPopWin('../../DemandPlan/PrintDemandPlan.aspx?id="+dpid+"','900px','800px');</script>");

            }
        }





    }
}
